﻿using CSharp大作业.Gobang.Game;
using GobangGameComponent;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace Game
{
    public partial class GobangGame : Form
    {
        public GameBoard board;
        public static int index;
        public GobangGame()
        {
            InitializeComponent();
            setGameBoard();
            board.StartGame();
        }

        private void setGameBoard()
        {
            board = new GameBoard();
            board.OnGameEnd += board_OnGameEnd;
            this.Controls.Add(board);
            Rectangle rec = this.ClientRectangle;
            int xPos = (rec.Width - board.Width) / 2;
            int yPos = (rec.Height - menu.Height - board.Height) / 2 + menu.Height;
            board.Left = xPos;
            board.Top = yPos;
        }
        public static Image CaptureControl(Control ctrl)
        {
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ctrl.Width, ctrl.Height);
            ctrl.DrawToBitmap(bmp, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
            return bmp;
        }
        private void DownLoad()
        {
            string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Pictures\\五子棋\\";//获取应用程序运行的路径
            string fileName = board.id_game.ToString() + "_" + board.name + ".png";//以日期命名文件名
            try
            {
                var image = CaptureControl(this);
                image.Save(filePath + fileName, ImageFormat.Png);

            }
            catch (Exception ex)
            {
                //异常处理
                MessageBox.Show(ex.Message, "截图失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        public static void DeleteAllFiles()
        {
            string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Pictures\\五子棋";//获取应用程序运行的路径
            DirectoryInfo directory = new DirectoryInfo(filePath);
            foreach (FileInfo file in directory.GetFiles())
            {
                file.Delete();
            }
        }
        public static void DeleteOneFile(int number)
        {
            string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Pictures\\五子棋";//获取应用程序运行的路径
            DirectoryInfo directory = new DirectoryInfo(filePath);
            foreach (FileInfo file in directory.GetFiles())
            {
                string index = "";
                int i = 0;
                while (file.FullName[i] < '0' || file.FullName[i] > '9') i++;
                while (file.FullName[i] != '_')
                {
                    index += file.FullName[i];
                    i++;
                }
                if(index==number.ToString())file.Delete();
            }
        }



        private void board_OnGameEnd(object sender, GobangGameComponent.GameEndEventArgs e)
        {

            Store form = new Store();
            form.label1.Text = e.Message;
            form.label1.Refresh();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.Cancel)
            {
                board.ifStore = false;
                board.db.deleteGame(board.id_game);
            }
            else
            {
                board.name = form.getText();
                board.ifStore = true;
                //截屏
                DownLoad();
            }
        }

        private void menuItemNewGame_Click(object sender, EventArgs e)
        {
            board.ReStartGame();
        }

        private void menuItemDebug_Click(object sender, EventArgs e)
        {
            GobangDebug frm = new GobangDebug(this.board);
            frm.Show();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void 游戏模式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GameMode form_mode = new GameMode();
            form_mode.checkBox1.Checked = board.isComputerfirst;
            form_mode.ShowDialog();
            if (form_mode.DialogResult == DialogResult.OK) {
                board.mode = form_mode.mode;
                //label1.Text = board.mode == 0 ? "简单模式" : "困难模式";
                if (board.mode == 0) label1.Text = "简单模式";
                if (board.mode == 1) label1.Text = "困难模式";
                if (board.mode == 2) label1.Text = "炼狱模式";
                label1.Refresh();
                board.isComputerfirst= form_mode.checkBox1.Checked;
                board.ReStartGame(); 
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }



        private void 复盘ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReProduce form = new ReProduce(board.id_game);
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                board.id_game = index;
                board.ReproduceGame(board.id_game);

            }
        }
    }
}
